package com.zsh.spring.test;

import com.zsh.spring.config.TxConfig;
import com.zsh.spring.entity.User;
import com.zsh.spring.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;

public class test {


    @Test
    public void testTransaction1(){
        Object object;
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);

        userService.accountMoney();
    }

    @Test
    public void testTransaction2(){
        Object object;
        ApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
        UserService userService = context.getBean("userService", UserService.class);

        userService.accountMoney();
    }

//    函数式分格创建对象
    @Test
    public void testTransaction3(){

        //1.创建对象
        GenericApplicationContext context = new GenericApplicationContext();

        context.refresh();

        context.registerBean("user",User.class,()->new User());

        User user = (User) context.getBean("user");

        System.out.println(user);
    }
}
